package hello;

import java.io.PrintStream;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.env.Environment;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;

@EnableGlobalMethodSecurity(securedEnabled = false)
@SpringBootApplication
@EnableDiscoveryClient
public class Application extends WebSecurityConfigurerAdapter {

	public static void main(String[] args) {
		SpringApplication sa = new SpringApplication(Application.class);
//		Banner b = new Banner() {
//			
//			@Override
//			public void printBanner(Environment paramEnvironment, Class<?> paramClass, PrintStream paramPrintStream) {
//				paramPrintStream.println("============");
//				paramPrintStream.println("Agree System");
//				paramPrintStream.println("============");
//				paramPrintStream.flush();
//			}
//		};
//		sa.setBanner(b);
		sa.run(args);
		
	}
	

	@Override
	public void configure(AuthenticationManagerBuilder auth) {
		try {
			auth.inMemoryAuthentication().withUser("user").password("1").roles("USER").and().withUser("admin")
					.password("1").roles("USER", "ADMIN");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void configure(HttpSecurity http) throws Exception {
		BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
		entryPoint.setRealmName("Spring Boot");
		http.exceptionHandling().authenticationEntryPoint(entryPoint);
		http.requestMatchers().antMatchers("/**").anyRequest().and().httpBasic().and().anonymous().disable().csrf()
				.disable();
	}
}